﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.Data.Entity;

namespace GPS.Controllers.GPS_DanhMuc
{
    public class GPS_DiemThiTotNghiepController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;

        #region Index
        public ActionResult Index(ViewModelSTHAMSO SearchModel, int? currentPage)
        {
            var entities = _db.Filter<STHAMSO>(o => (SearchModel.tTHAMSO_MA == null || o.THAMSO_MA.StartsWith(SearchModel.tTHAMSO_MA))
                                                 && (SearchModel.tTHAMSO_ACTIVE == null || o.THAMSO_ACTIVE == SearchModel.tTHAMSO_ACTIVE));
            ViewBag.TotalRow = entities.Count();
            var model = new List<STHAMSO>();
            foreach (var currentEntity in entities)
            {
                model.Add(Mapper.Map<STHAMSO>(currentEntity));
            }
            Int16 icount = 1;
            foreach (var item in model)
            {
                item.STT = icount;
                item.THAMSO_ACTIVE = item.THAMSO_ACTIVE == "1" ? "Hiệu lực" : "Không hiệu lực";
                icount++;
            }
            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View(SearchModel);
        }

        [HttpPost]
        public void initialCategorySearchAction(STHAMSO viewModel)
        {
            viewModel.THAMSO_ACTIVE = viewModel.THAMSO_ACTIVE == "1" ? "Hiệu lực" : "Không hiệu lực";
        }
        #endregion

        #region Detail
        public ActionResult Detail(string id)
        {
            CrudModelSTHAMSO _model = new CrudModelSTHAMSO();
            STHAMSO model = _db.GetOne<STHAMSO>(o => o.THAMSO_MA == id);
            GPS.COMMON.Helpers.CopyObject<CrudModelSTHAMSO>(model, ref _model);
            initialCategoryDetailAction(_model);
            return PartialView("Detail", _model);
        }

        public void initialCategoryDetailAction(CrudModelSTHAMSO view)
        {
            view.THAMSO_ACTIVE = view.THAMSO_ACTIVE == "1" ? "Hiệu lực" : "Không hiệu lực";

        }
        #endregion
    }

}
